package com.qiangesoft.blog.file.core;

import com.qiangesoft.blog.common.constants.Constants;
import com.qiangesoft.blog.file.core.local.LocalFileService;
import com.qiangesoft.blog.file.model.FileInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件本地存储
 *
 * @author qiange
 * @date 2023-04-26
 */
@Component("FILE_LOCAL")
public class LocalFileStoreImpl implements FileStoreStrategy {

    @Autowired
    private LocalFileService localFileService;

    @Override
    public FileInfoVo uploadFile(MultipartFile file) throws Exception {
        return localFileService.uploadFile(file, null);
    }

    @Override
    public FileInfoVo uploadFile(MultipartFile file, String path) throws Exception {
        return localFileService.uploadFile(file, path);
    }

    @Override
    public FileInfoVo uploadFile(InputStream inputStream, String contentType) throws Exception {
        return localFileService.uploadFile(inputStream, contentType, null);
    }

    @Override
    public FileInfoVo uploadFile(InputStream inputStream, String contentType, String path) throws Exception {
        return localFileService.uploadFile(inputStream, contentType, path);
    }

    @Override
    public List<FileInfoVo> batchUploadFile(MultipartFile[] files) throws Exception {
        List<FileInfoVo> fileInfoVos = new ArrayList<>();
        for (MultipartFile file : files) {
            FileInfoVo fileInfoVo = localFileService.uploadFile(file, null);
            fileInfoVos.add(fileInfoVo);
        }
        return fileInfoVos;
    }

    @Override
    public List<FileInfoVo> batchUploadFile(MultipartFile[] files, String path) throws Exception {
        List<FileInfoVo> fileInfoVos = new ArrayList<>();
        for (MultipartFile file : files) {
            FileInfoVo fileInfoVo = localFileService.uploadFile(file, path);
            fileInfoVos.add(fileInfoVo);
        }
        return fileInfoVos;
    }

    @Override
    public void downloadFile(String key, HttpServletRequest request, HttpServletResponse response) throws Exception {
        byte[] bytes = localFileService.downloadByFilePath(key);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/force-download");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(key.substring(key.lastIndexOf(Constants.SLASH) + 1), "utf-8"));
            try (ServletOutputStream stream = response.getOutputStream()) {
                stream.write(bytes);
                stream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteFile(String key) throws Exception {
        localFileService.deleteByFilePath(key);
    }
}
